package com.work.keep.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.work.keep.R;
import com.work.keep.datas.TopicActivityData;
import com.work.keep.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/22.
 */
public class MyTopicActivityAdapter extends RecyclerView.Adapter<MyTopicActivityAdapter.MyViewHolder> {
    private Context context;
    private List<TopicActivityData.DataEntity> topicData;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;

    public MyTopicActivityAdapter(Context context) {
        this.context = context;
        topicData = new ArrayList<>();
        volleySingleton = VolleySingleton.getInstance();
        imageLoader = volleySingleton._getImageLoader();
    }

    public void addData(List<TopicActivityData.DataEntity> topicData) {
        this.topicData = topicData;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_topic_recycler, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.topicItemPhoto.setDefaultImageResId(R.mipmap.photo);
        holder.topicItemPhoto.setErrorImageResId(R.mipmap.photo);
        holder.topicItemPhoto.setImageUrl(topicData.get(position).getPhoto() + ".jpg", imageLoader);
        holder.topicActivityName.setText(topicData.get(position).getName());
        holder.topicActivityContent.setText(topicData.get(position).getContent());
        holder.topicActivityCount.setText(topicData.get(position).getCount() + "");
    }


    @Override
    public int getItemCount() {
        return topicData.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        NetworkImageView topicItemPhoto;
        TextView topicActivityName, topicActivityContent, topicActivityCount;

        public MyViewHolder(View itemView) {
            super(itemView);
            topicActivityName = (TextView) itemView.findViewById(R.id.topicActivityName);
            topicItemPhoto = (NetworkImageView) itemView.findViewById(R.id.topicItemPhoto);
            topicActivityContent = (TextView) itemView.findViewById(R.id.topicActivityContent);
            topicActivityCount = (TextView) itemView.findViewById(R.id.topicActivityCount);
        }
    }
}
